package com.swang.pms.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import com.swang.pms.model.constants.CommonConstants;
import com.swang.pms.model.dto.common.SysDictionaryDto;
import com.swang.pms.model.entity.CurrentKey;
import com.swang.pms.model.entity.ManageUser;
import com.swang.pms.model.vo.ResultMessage;
import com.swang.pms.web.listener.SessionListener;

/**
 * @ClassName: BaseController
 * @Description: 基础服务控制层
 * @author 王泽
 * @date 2017年3月16日 下午9:45:51
 */
public class BaseController {

    private static final Logger LOGGER           = Logger.getLogger(BaseController.class);

    protected static String     templateLocation = "template/";

    protected String            message;                                                  // 操作成功消息

    protected String            errorMessage;                                             // 操作失败消息

    public Integer              pageSize         = 10;

    public Integer              pageIndex        = 1;

    protected String            menu;

    public long                 imgMaxSize       = 100L * 1024L;                          // 上传图片统一大小100kb

    public long getImgMaxSize() {
        return imgMaxSize;
    }

    public void setImgMaxSize(long imgMaxSize) {
        this.imgMaxSize = imgMaxSize;
    }

    protected ManageUser getSessionUser(HttpServletRequest request) {
        Object obj = request.getSession().getAttribute(CommonConstants.SESSION_USER_KEY);
        if (obj != null) {
            return (ManageUser) obj;
        }
        return null;
    }

    protected void setSessionUser(HttpServletRequest request, ManageUser admin) {
        request.getSession().setAttribute(CommonConstants.SESSION_USER_KEY, admin);
        if (admin != null) {
            SessionListener.addSessionUser(admin.getId() + "", request.getSession());
        }
    }

    protected String getParameter(HttpServletRequest request, String paramName) {
        return request.getParameter(paramName);
    }

    protected String getCurrentMenu(HttpServletRequest request) {
        return request.getParameter("m");
    }

    protected ModelAndView redirect(HttpServletRequest request, String path) {
        return new ModelAndView("redirect:" + path + "?m=" + getCurrentMenu(request));
    }

    public void setMessage(ModelMap model, String message) {
        model.put("message", message);
    }

    public void setErrorMessage(ModelMap model, String message) {
        model.put("errorMessage", message);
    }

    public static String getTemplateLocation() {
        return templateLocation;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(Integer pageIndex) {
        this.pageIndex = pageIndex;
    }

    public String getMenu() {
        return menu;
    }

    public void setMenu(String menu) {
        this.menu = menu;
    }

    public static String getVcode(HttpServletRequest request) {
        if (request == null) {
            return "";
        }
        return (String) request.getSession().getAttribute("YUN_SESSION_KEY");
    }

    // 验证码
    public static void setVcode(HttpServletRequest request, String capText) {
        if (capText == null || request == null) {
            return;
        }
        request.getSession().setAttribute("YUN_SESSION_KEY", capText);
    }

    public static CurrentKey getCurrentKey(HttpServletRequest request) {
        if (request == null) {
            return null;
        }
        if (request.getSession().getAttribute(CommonConstants.CURRENT_KEY) == null) {
            return null;
        }
        return (CurrentKey) request.getSession().getAttribute(CommonConstants.CURRENT_KEY);
    }

    public static String getComboDataFromDictList(List<SysDictionaryDto> sysDictList, String selectValue){
        StringBuilder resultStr = new StringBuilder("([[\'"+selectValue+ "\', \'--选择--\'],");
        if(CollectionUtils.isEmpty(sysDictList)){
            return "([[\'"+selectValue+ "\', \'--选择--\']])";
        } else {
            for (int i = 0; i < sysDictList.size(); i++) {
                SysDictionaryDto dto = sysDictList.get(i);
                resultStr.append("[\'");
                resultStr.append(dto.getDictCode());
                resultStr.append("\',\'");
                resultStr.append(dto.getDictName());
                resultStr.append("\']");
                if(i != sysDictList.size()-1) {
                    resultStr.append(",");
                }
            }
            resultStr.append("])");
        }
        return resultStr.toString();
    }

    public static String getComboData(List<SysDictionaryDto> sysDictList, String selectValue) {
        StringBuilder returnStr = new StringBuilder("([[\'").append(selectValue).append("\', \'--请选择--\'],");
        if (sysDictList == null || sysDictList.isEmpty()) {
            return "([[\'" + selectValue + "\', \'--请选择--\']])";
        } else {
            for (int i = 0; i < sysDictList.size(); i++) {
                SysDictionaryDto dto = sysDictList.get(i);
                returnStr.append("[\'");
                returnStr.append(dto.getDictCode());
                returnStr.append("\',\'");
                returnStr.append(dto.getDictName());
                returnStr.append("\']");
                if (i != sysDictList.size() - 1) {
                    returnStr.append(",");
                }
            }
            returnStr.append("])");
        }
        return returnStr.toString();
    }

    public static Boolean checkParam(HttpServletRequest request, CurrentKey parm) {
        return true;
    }

    public ResultMessage infoSecCheck(String biz, String content) {
        ResultMessage resultMessage = new ResultMessage();
        return resultMessage;
    }

}
